package edu.kit.ksri.db.mturk.connectionhandlers;

import java.util.ArrayList;
import java.util.Calendar;

import org.apache.log4j.Logger;

import com.amazonaws.mturk.requester.Assignment;
import com.amazonaws.mturk.requester.HIT;

import edu.kit.ksri.db.mturk.simulations.correctness.CorrSimMTurk;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
public class CorrSimHITManager extends AbstractHITManager {

	private static final Logger basiclog = Logger
			.getLogger("basic." + CorrSimHITManager.class);

	@Override
	public double accountBalance() {
		return 9999999999999999999999.99;
	}

	@Override
	public boolean approveAssignment(String assignmentid, String feedback) {
		basiclog.info("Approved assignment " + assignmentid);
		return true;
	}

	@Override
	protected HIT[] createHITsInternal(String inputDirectory, String questionFile,
			String propertiesFile) {
		basiclog.info("Simulating HIT creation using"
				+ " the XML files in siminput directory as input for CorrSimMTurk.");
		HIT[] res = null;
		try {
			//read the HITs from the input directory
			CorrSimMTurk.initialize(inputDirectory);
			//get the HIT IDs
			String[] hitids = CorrSimMTurk.getHitIds();
			res = new HIT[hitids.length];
			int counter = 0;
			//Create HITs
			for (String s : hitids) {
				res[counter] = new HIT();
				res[counter].setHITTypeId("SIM_HITTypeId");
				res[counter].setHITId(s);
				res[counter].setCreationTime(Calendar.getInstance());
				counter++;
				//Create current HIT
				CorrSimMTurk.addHIT(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return res;
	}

	@Override
	public boolean deleteHit(String hitid) {
		throw new RuntimeException("Unsupported operation deleteHITs()");
	}

	@Override
	protected boolean extendHitInternal(String hitId) {
		return CorrSimMTurk.extendHIT(hitId);
	}

	@Override
	public String[] getAllHITIDs() {
		return CorrSimMTurk.getHitIds();
	}

	@Override
	public HIT[] getAllHITs(){
		throw new RuntimeException("Unsupported operation getAllReviewableHITIDs()");
	}

	@Override
	public HIT[] getAllHITsForHITType(){
		throw new RuntimeException("Unsupported operation getAllReviewableHITIDs()");
	}

	@Override
	public Assignment[] getAssignmentsForHITID(String hitid) {
		ArrayList<Assignment> answers = new ArrayList<Assignment>();
		try {
			//Get as many answers as available
			while (true) {
				answers.add(CorrSimMTurk.getNextAnswerForHit(hitid));
			}
		} catch (Exception e) {
			// queue is empty now => don't do anything
		}
		return answers.toArray(new Assignment[answers.size()]);
	}

	@Override
	public int getNumberOfCompletedHITs(String hittypeid) {
		throw new RuntimeException(
				"Unsupported operation getNumberOfCompletedHITs()");
	}

	@Override
	public int getNumberOfHITs(String hittypeid) {
		throw new RuntimeException("Unsupported operation getNumberOfHITs()");
	}

	@Override
	public boolean hasEnoughFund() {
		return true;
	}

	@Override
	public boolean rejectAssignment(String assignmentid, String feedback) {
		basiclog.info("Rejected assignment " + assignmentid);
		return true;
	}

	@Override
	public String[] getAllReviewableHITIDs() {
		throw new RuntimeException("Unsupported operation getAllReviewableHITIDs()");
	}

}
